import { any } from "../../../core/logic";
import { Handler } from "./Handler";
import { Channel, Sampler} from "../../../component/animation/index";
class PanMouse extends Handler{
    constructor(opts={}){
        super(opts);
        this.button=any(opts.panButton,0);
        this.easing=any(opts.easing,"easeOut_Cubic");
        this.dual=any(opts.dual,1.0);//1s
        this.speed=any(opts.speed,0.2);
        this.restrict=opts.restrict;
        this.cur=null;//当前值
        this.pre=null;
        this.register();
    }
    register(){
        this.on("mousedown",this.start);
        this.on("mousemove",this.update);
        this.on("mouseup",this.end);
    }
    start(event){
        if(!this.avalid){
            return;
        }
        this.animation.stop();
        this.animation.clear();
        if(event.data.button===this.button){
            this.alive=true;
            this.cur=this.pre={
                pixel:event.data.pixel,
                time:event.data.time
            };
        }
        
    }
    update(event){
        if(!this.alive){
            return;
        }
        this.pre=this.cur;
        this.cur={
            pixel:event.data.pixel,
            time:event.data.time
        }
        //计算
        let cpos=this.target.pixelToWorld([this.cur.pixel[0],this.target.size[1]/2+this.cur.pixel[1]-this.pre.pixel[1]]);
        let ppos=this.target.pixelToWorld([this.pre.pixel[0],this.target.size[1]/2]);
        let to=[this.target.at.x-(cpos.x-ppos.x),this.target.at.y-(cpos.y-ppos.y)];
        if(this.restrict){
            to[0]=Math.max(this.restrict[0],to[0]);
            to[0]=Math.min(this.restrict[2],to[0]);
            to[1]=Math.max(this.restrict[1],to[1]);
            to[1]=Math.min(this.restrict[3],to[1]);
        }
        this.target.at=to;
    }
    end(event){
        if(!this.alive){
            return false;
        }
        this.alive=false;
        if(this.dual<=0){//不执行动画
            return false;
        }
        const t=this.cur.time-this.pre.time;
        if(t==0){
            return false;
        }
        //v=at;s=vt-at²/2  t(已知) v(已知)  推导:s=vt-v/t*t²/2=vt/2;
        let cpos=this.target.pixelToWorld([this.cur.pixel[0],this.target.size[1]/2+this.cur.pixel[1]-this.pre.pixel[1]]);
        let ppos=this.target.pixelToWorld([this.pre.pixel[0],this.target.size[1]/2]);
        const diff=[cpos.x-ppos.x,cpos.y-ppos.y];
        const v=[diff[0]/t,diff[1]/t];
        const s=[v[0]*this.dual*this.speed/2,v[1]*this.dual*this.speed/2]; 
        let to=[this.target.at.x-s[0],this.target.at.y-s[1],this.target.at.z];
        if(this.restrict){
            to[0]=Math.max(this.restrict[0],to[0]);
            to[0]=Math.min(this.restrict[2],to[0]);
            to[1]=Math.max(this.restrict[1],to[1]);
            to[1]=Math.min(this.restrict[3],to[1]);
        }
        const sampler=new Sampler([0,this.dual],[this.target.at.toArray(),to],"LINEAR",this.easing);
        let channel=new Channel(this.target,"at");
        channel.add(sampler);
        this.animation.add(channel);
        this.animation.start();
    }
}
export {PanMouse}